<template>
  <a-modal
    title="新增物料清单"
    width="80%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="父项产品"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <pro-select
            placeholder="请选择父项产品"
            v-decorator="['parentId', { rules: [{ message: '请选择父项产品！' }]}]"
          />
        </a-form-item>
        <a-form-item
          label="子项产品"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <pro-select
            placeholder="请选择子项产品"
            v-decorator="['subitemId', { rules: [{ message: '请选择子项产品！' }]}]"
          />
        </a-form-item>
        <a-form-item
          label="单位用量"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback>
          <a-input-number
            style="width: 100%"
            :min="1"
            v-decorator="['unitCon', {rules: [{required: true, message: '请输入单位用量'}]}]" />
        </a-form-item>
        <a-form-item
          label="备注"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-textarea
            placeholder="请输入备注"
            v-decorator="['remarks']"
            :auto-size="{ minRows: 3, maxRows: 6 }"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { bomAdd, bomPage } from '@/api/modular/main/bom/bomManage'
import ProSelect from '@/components/MyComp/ProSelect'
export default {
  components: {
    ProSelect
  },
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 }
      },
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      bomTree: [] // 存放从后端取回的数据数组
    }
  },
  methods: {
    // 初始化方法
    async add(record) {
        const bomTree = await this.getBomTree()
        this.visible = true
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              parentId: bomTree.rows.length !== 0 ? this.bomTree.rows[0].parentId : '',
              subitemId: bomTree.rows.length !== 0 ? this.bomTree.rows[0].subitemId : '',
              unitCon: 1
            }
          )
        }, 100)
        this.visible = true
    },
    // 获取物料表单列表
    async getBomTree() {
      var promise = bomPage().then((res) => {
        this.formLoading = false
        if (!res.success) {
          this.bomTree = []
          return
        }
        this.bomTree = res.data
      })
      await promise
      return this.bomTree
    },
    /**
     * 提交表单
     */
    handleSubmit() {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          for (const key in values) {
            if (typeof (values[key]) === 'object' && values[key] != null) {
              values[key] = JSON.stringify(values[key])
            }
          }
          bomAdd(values).then((res) => {
            if (res.success) {
              this.$message.success('新增成功')
              this.confirmLoading = false
              this.$emit('ok', values)
              this.handleCancel()
            } else {
              this.$message.error(res.message)// + res.message
            }
          }).finally((res) => {
            this.confirmLoading = false
          })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
